package com.example.xiaoxueqixiangmu.jiemian;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xiaoxueqixiangmu.R;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OrderControlActivity extends AppCompatActivity {
    private TextView tv_userName,tv_cheming2,tv_riqi,textView2;
    private Button btn_cancle,btn_zhifu;
    private List<HashMap<String, Object>> OC_dataList;
    private SimpleAdapter adapter1;
    private ListView OC_listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_control_layout);//activity_order_control
        btn_cancle=findViewById(R.id.btn_cancle);
        btn_zhifu=findViewById(R.id.btn_zhifu);
        ActionBar actionBar=getActionBar();
        if (actionBar!=null){
            actionBar.setIcon(R.drawable.contact_list_icon);
            actionBar.setDisplayUseLogoEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }


        OC_dataList = new ArrayList<HashMap<String, Object>>();
        final HashMap<String, Object> map1,map2,map3;
            map1 = new HashMap<String, Object>();
            map1.put("tv_userName", "冬不拉");
            map1.put("tv_cheming2", "G180");
            map1.put("tv_riqi", "2021-9-2");
            map1.put("textView2", "6车50号");
            OC_dataList.add(map1);


        map2 = new HashMap<String, Object>();
        map2.put("tv_userName", "陈伟飞");
        map2.put("tv_cheming2", "G108");
        map2.put("tv_riqi", "2021-9-2");
        map2.put("textView2", "6车51号");
        OC_dataList.add(map2);


       /* map3 = new HashMap<String, Object>();
        map3.put("tv_userName", "阔乐");
        map3.put("tv_cheming2", "AHG5");
        map3.put("tv_riqi", "2021-7-7");
        map3.put("textView2", "3车03号");
        OC_dataList.add(map3);
*/

        adapter1 = new SimpleAdapter(this, OC_dataList, R.layout.order_control_item_layout,
                new String[]{"tv_userName", "tv_cheming2", "tv_riqi","textView2"}, new
                int[]{R.id.tv_userName, R.id.tv_cheming2, R.id.tv_riqi,  R.id.textView2});
        OC_listView = findViewById(R.id.order_control_listView);
        OC_listView.setAdapter(adapter1);

        OC_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                    if (position>=0){
                        new AlertDialog.Builder(OrderControlActivity.this)
                                .setMessage("确定取消订单？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        //清除单个的item数据
                                        OC_dataList.remove(position);
                                        adapter1.notifyDataSetChanged();
                                    }
                                })
                                .setNegativeButton("取消",null)
                                .create()
                                .show();


                        Toast.makeText(OrderControlActivity.this, "你点击了第"+(position+1)+"项", Toast.LENGTH_SHORT).show();

                    }
            }
        });


        //取消订单的按钮监听器
        //目的：清除listView的数据
        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (OC_dataList!=null){
                    OC_dataList.clear();

                }
                if (adapter1!=null){
                    adapter1.notifyDataSetChanged();
                }
                Toast.makeText(OrderControlActivity.this,"成功取消订单",Toast.LENGTH_SHORT).show();
                btn_zhifu.setVisibility(View.GONE);
            }

        });

        //确认支付的按钮监听器
        btn_zhifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(OrderControlActivity.this,OrderGaiActivity.class);
                intent.putExtra("list",(Serializable) OC_dataList);
                startActivity(intent);
                Toast.makeText(OrderControlActivity.this,"确认支付成功",Toast.LENGTH_SHORT).show();
            }
        });


    }


}